﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Business.Models;
using Website.Models;
using Business.DAO;

namespace Website.Controllers
{
    public class KhoiLopController : Controller
    {
        KhoiLopDAO dao = new KhoiLopDAO();
        IEnumerable<KhoiLop> model = null;

        int IdTruongHoc { get; set; }

        private bool CheckLogin()
        {
            try
            {
                QuanTri quanTri = Utils.CheckLogin_QuanTri();
                if (quanTri != null)
                {
                    IdTruongHoc = quanTri.NhanVien.IdTruongHoc;
                    ViewBag.IdTruongHoc = IdTruongHoc;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Website.Models.Utils.RoleException ex)
            {
                throw ex;
            }
        }

        public ActionResult Index(int ID = 0, String Function = "")
        {
            if (CheckLogin())
            {

                ViewBag.Function = Function;

                if (Function != "" && Function == "Delete" && ID != 0)
                {
                    ViewBag.DeleteResult = dao.Delete(ID);
                    //Tra form lai dang Create Form
                    ViewBag.Function = "Create";
                }

                model = dao.GetByTruongHoc(IdTruongHoc); 
                return View(model);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        [HttpPost]
        public ActionResult Index(KhoiLop daiLy, String Function = "")
        {
            ViewBag.Function = Function;

            if (Function == "Edit")
            {
                Edit(daiLy);
            }
            else if (Function == "Create")
            {
                Create(daiLy);
            }

            model = dao.GetByTruongHoc(IdTruongHoc);
            return View(model);
        }

        public ActionResult Create(int IdKhoiLop)
        {
            ViewBag.IdTruongHoc = IdKhoiLop;
            return View();
        }

        private void Create(KhoiLop daiLy)
        {
            try
            {
                ViewBag.CreateResult = dao.Create(daiLy);
            }
            catch (Exception ex)
            {
            }
        }

        public ActionResult GridViewPartial()
        {
            try
            {
                model = dao.GetByTruongHoc(IdTruongHoc);
            }
            catch (Exception ex)
            {
            }
            return PartialView("GridView", model);
        }

        public ActionResult Edit(int IdKhoiLop)
        {
            KhoiLop daiLy = null;
            try
            {

                if (IdKhoiLop != 0)
                {
                    model = dao.GetByTruongHoc(IdTruongHoc);
                    daiLy = model.Where(daily => daily.IdKhoiLop == IdKhoiLop).First();
                }
                else
                {
                    daiLy = new KhoiLop();
                }
            }
            catch (Exception ex)
            {
            }
            return View(daiLy);
        }

        private void Edit(KhoiLop daiLy)
        {
            try
            {
                if (daiLy.IdKhoiLop != 0)
                {
                    ViewBag.EditResult = dao.Edit(daiLy);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
